package cn.itsource.itsource.service.impl;

import cn.itsource.itsource.domain.*;
import cn.itsource.itsource.mapper.*;
import cn.itsource.itsource.query.WangLinQuery;
import cn.itsource.itsource.service.IWangLinService;
import cn.itsource.itsource.util.AjaxResult;
import cn.itsource.itsource.util.PageList;
import cn.itsource.itsource.vo.PetVo;
import cn.itsource.itsource.vo.ProductVo;
import com.alipay.api.domain.Product;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;


@Service
@Transactional
public class WangLinServiceImpl implements IWangLinService {
    @Autowired
    private WangLinMapper wangLinMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ShopMapper shopMapper;


    @Override
    public PageList findPage(WangLinQuery query) {
        //1.拼接条件
        Example example = new Example(Pet.class);
        Example.Criteria and = example.and();
        if (StringUtils.isNotBlank(query.getName())) {
            and.andLike("name", "%" + query.getName() + "%");
        }
        if (query.getState() != null) {
            and.andEqualTo("state", query.getState());
        }
        //分页条件
        PageHelper.startPage(query.getCurrPage(), query.getPageSize());
        PageInfo<WangLin> pageInfo = new PageInfo<>(wangLinMapper.selectByExample(example));
        return new PageList(pageInfo.getTotal(), pageInfo.getList());

    }

    @Override
    public AjaxResult saveOrUpdate(WangLin wangLin) {
        wangLinMapper.save(wangLin);

        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult del(Long id){
        wangLinMapper.del(id);
        return AjaxResult.success("删除成功");
    }

    @Override
    public PageList queryHomePage(WangLinQuery query) {
        //【已上架】
        Example example = new Example(Pet.class);
        Example.Criteria and = example.and();
        //固定条件：已上架
        and.andEqualTo("state", 1);//1表示：上架宠物

        //进行分页查询
        PageHelper.startPage(query.getCurrPage(), query.getPageSize());
        PageInfo<WangLin> pageInfo = new PageInfo<>(wangLinMapper.selectByExample(example));


        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public ProductVo getDetailById(Long id) {
        ProductVo productVo = new ProductVo();
        WangLin wangLin = wangLinMapper.selectByPrimaryKey(id);
        ProductDetail productDetail = productDetailMapper.selectByPrimaryKey(id);
        //Shop tShop = shopMapper.selectByPrimaryKey(tPet.getShopId());
        productVo.setWangLin(wangLin);
        productVo.setProductDetail(productDetail);
        //petVo.setShop(tShop);
        return productVo;

    }

    @Override
    public WangLin findProductById(Long id) {
        return wangLinMapper.selectByPrimaryKey(id);
    }
}
